package com.wangsd.web.coupon.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.Merchant;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.mapper.MerchantMapper;
import com.wangsd.web.coupon.service.CompanyService;
import com.wangsd.web.coupon.service.MerchantService;
import com.wangsd.web.coupon.service.StoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-20
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Resource
    MerchantMapper merchantMapper;

    @Autowired
    CompanyService companyService;

    @Autowired
    StoreService storeService;

    @Override
    public void addOrUpdate(Merchant merchant) {
        if (merchant.getId() != null) {
            merchantMapper.updateById(merchant);
        } else {
            merchantMapper.insert(merchant);

            //保留检索CODE
            Company company = companyService.getById(merchant.getCompanyId());
            merchant.setCode(company.getCode() + "-" + merchant.getId());
            merchantMapper.updateById(merchant);
        }
    }

    @Override
    public IPage<Merchant> selectPage(String code, Integer pageNum, Integer pageSize, Merchant merchant) {
        Page<Merchant> page = new Page<>(pageNum, pageSize);
        IPage<Merchant> resultIPage = merchantMapper.selectListXml(page, merchant, code);
        return resultIPage;
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        List<Merchant> list = merchantMapper.selectList(queryWrapper);
        for (Merchant merchant : list) {
            Map map = new HashMap();
            map.put("code", merchant.getCode());
            map.put("id", merchant.getId());
            map.put("name", merchant.getName());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public JSONArray selectByCompanyId(Long companyId) {
        /*查询商圈信息*/
        Company company = companyService.getById(companyId);

        if (company == null) {
            return null;
        }

        /*查询商户信息*/
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        List<Merchant> merchants = merchantMapper.selectList(queryWrapper);

        /*查询门店信息*/
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.likeRight("code", company.getCode());
        List<Store> list = storeService.list(storeQueryWrapper);
        Map<Long, List<Store>> merchantCollect = null;
        if (list != null) {
            merchantCollect = list.stream().collect(Collectors.groupingBy(Store::getMerchantId));
        }

        /*拼装前端所需数据*/
        JSONArray resultArray = new JSONArray();
        if (merchants != null && merchantCollect != null) {
            for (Merchant merchant : merchants) {
                List<Store> stores = merchantCollect.get(merchant.getId());
                if (stores != null) {



                    JSONObject merchantObject = new JSONObject();
                    merchantObject.put("title", merchant.getName());
                    merchantObject.put("key", merchant.getCode());
                    merchantObject.put("value", merchant.getCode());
                    merchantObject.put("disabled", true);
                    JSONArray storeArray = new JSONArray();
                    for (Store store : stores) {
                        JSONObject storeObject = new JSONObject();
                        storeObject.put("title", store.getName());
                        storeObject.put("key", store.getCode());
                        storeObject.put("value", store.getCode());
                        storeArray.add(storeObject);
                    }

                    merchantObject.put("children", storeArray);
                    resultArray.add(merchantObject);
                }
            }
        }
        return resultArray;
    }

    @Override
    public List<Merchant> selectMerchantByCompanyId(Long companyId) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        List<Merchant> merchants = merchantMapper.selectList(queryWrapper);
        return merchants;
    }

    @Override
    public Merchant selectRepeat(String name, Long id, Long companyId) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("company_id", companyId);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return merchantMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Merchant> selectAllByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Merchant> list = merchantMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Merchant> selectByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Merchant> list = merchantMapper.selectList(queryWrapper);
        return list;
    }

}
